package org.petstore.web.servlet;

import org.petstore.domain.Account;
import org.petstore.service.AccountService;
import org.petstore.service.impl.AccountServiceImpl;
import org.petstore.web.servlet.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static org.petstore.web.servlet.util.ServletUtil.LOGIN_FORM;

/**
 * @projectName: MyWebProject
 * @package: org.petstore.web
 * @className: RegisterServlet
 * @author: Eric
 * @description: TODO
 * @date: 2021/10/10 11:44
 * @version: 1.0
 */
public class RegisterServlet extends HttpServlet {

    private static final String REGISTER_FORM = "/WEB-INF/jsp/account/NewAccountForm.jsp";
    private final AccountService accountService = MyUtil.getApplicationContext().getBean("accountService", AccountService.class);

    String msg;
    private final Account user = new Account();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if (request.getParameter("username") == null || request.getParameter("password") == null || request.getParameter("username").equals("") || request.getParameter("password").equals("")) {
            msg = "用户名或密码为空，请重新输入";
            request.setAttribute("msg", msg);
            request.getRequestDispatcher(REGISTER_FORM).forward(request, response);
        }
        if (!request.getParameter("password").equals(request.getParameter("password2"))) {
            msg = "两次密码不一致，请重新输入";
            request.setAttribute("msg", msg);
            request.getRequestDispatcher(REGISTER_FORM).forward(request, response);
        }
        user.setAddress1(request.getParameter("address1"));
        user.setAddress2(request.getParameter("address2"));
        user.setCity(request.getParameter("city"));
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user.setPhone(request.getParameter("phone"));
        user.setLanguagePreference(request.getParameter("languagePreference"));
        user.setBannerOption(request.getParameter("myBanner") == ("myBanner"));
        user.setCountry(request.getParameter("county"));
        user.setEmail(request.getParameter("email"));
        user.setFavouriteCategoryId(request.getParameter("categoryPreference"));
        user.setFirstName(request.getParameter("firstName"));
        user.setLastName(request.getParameter("lastName"));
        user.setListOption(request.getParameter("myList") == ("myList"));
        user.setState(request.getParameter("state"));
        user.setStatus(request.getParameter("status"));
        user.setZip(request.getParameter("zip"));
        int count = accountService.register(user);

        if (count == -1) {
            msg = "该用户名已注册，请重新输入用户名";
            request.setAttribute("msg", msg);
            request.getRequestDispatcher(REGISTER_FORM).forward(request, response);
        }
        if (count == 0) {
            msg = "貌似出问题了，请重试";
            request.setAttribute("msg", msg);
            request.getRequestDispatcher(REGISTER_FORM).forward(request, response);
        }

        msg = "注册成功，请登录";
        request.setAttribute("msg", msg);

        request.getRequestDispatcher(LOGIN_FORM).forward(request, response);
    }
//    private static final String LOGIN_FORM = "WEB-INF/jsp/login.jsp";
//    private static final String REGISTER_FORM = "WEB-INF/jsp/register.jsp";
//
//    private UserService userService = new UserServiceImpl();
//    private String msg;
//    User user = new User();
//
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        doPost(req, resp);
//    }
//
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
//        if (!isParameterLegal(req)) {
//            //参数不合法
//            req.getRequestDispatcher(REGISTER_FORM).forward(req, resp);
//        }
//        //参数合法
//        if (!userService.Register(user)) {
//            //todo
//            //注册失败
//            //请稍后再试
//            return;
//        }
//        //注册成功
//        req.setAttribute("user", user);
//
//        req.getRequestDispatcher(LOGIN_FORM).forward(req, resp);
//    }
//
//    public boolean isParameterLegal(HttpServletRequest req) throws ServletException, IOException {
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        String gender = req.getParameter("gender");
//        String email = req.getParameter("email");
//        int age;
//        if (req.getParameter("username") == null || req.getParameter("username").equals("")) {
//            msg = "用户名不能为空";
//            req.setAttribute("usernameMsg", msg);
//            return false;
//        }
//        if (req.getParameter("password") == null || req.getParameter("password").equals("")) {
//            msg = "密码不能为空";
//            req.setAttribute("passwordMsg", msg);
//            return false;
//        }
//        if (req.getParameter("password1") == null || req.getParameter("password1").equals("")) {
//            msg = "第二次密码不能为空";
//            req.setAttribute("passwordAgainMsg", msg);
//            return false;
//        }
//        if (!req.getParameter("password1").equals(password)) {
//            msg = "两次密码不正确";
//            req.setAttribute("passwordAgainMsg", msg);
//            return false;
//        }
//        try {
//            age = Integer.parseInt(req.getParameter("age"));
//        } catch (Exception e) {
//            msg = "年龄输入不正确";
//            req.setAttribute("ageMsg", msg);
//            return false;
//        }
//        //需要对数据库操作的放后面 减少对服务器的负担 提高效率
//        //todo AJAX
//        if (userService.SelectByName(username)) {
//            msg = "用户名已存在";
//            req.setAttribute("usernameMsg", msg);
//            return false;
//        }
//        user.setUsername(username);
//        user.setPassword(password);
//        user.setAge(age);
//        user.setEmail(email);
//        //三目运算符确定单选框的值
//        user.setGender(gender.equals("male") ? "MAN" : "WOMAN");
//        return true;
//    }

}
